package updater;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.ParseException;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.json.JSONWriter;

public class MapShareVersionInfo {
	MapShareVersionNumber version;
	File infoFile;

	public void loadFile(File f) throws IOException, JSONException, ParseException {
		infoFile = f;
		if(infoFile.createNewFile())
			initFile();
		
		FileInputStream fis = new FileInputStream(infoFile);
		JSONObject jo = new JSONObject(new JSONTokener(fis));
		version = new MapShareVersionNumber(jo.getString("Version"));
	}

	private void initFile() throws IOException {
		FileOutputStream fos = new FileOutputStream(infoFile);
		OutputStreamWriter osw = new OutputStreamWriter(fos);
		JSONWriter jw = new JSONWriter(osw);
		try {
			jw.object()
				.key("Version")
				.value("1.0.0.0")
			.endObject();
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		osw.close();
		fos.close();
	}

	public MapShareVersionNumber parseVersionNumber(String string) throws ParseException{
		version = new MapShareVersionNumber(string);
		return version;
	}
}